//
//  ViewController.m
//  封装轮播图
//
//  Created by 李昊林 on 16/8/4.
//  Copyright © 2016年 李昊林. All rights reserved.
//

#import "ViewController.h"
#import "AppDelegate.h"
#define FRAME [UIScreen mainScreen].bounds
#define WIDTH FRAME.size.width
#define HEIGHT FRAME.size.height
@interface ViewController ()<UIScrollViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UIPageControl *page;
@property (nonatomic, assign) NSInteger index;
@property (nonatomic,retain) UIScrollView *scrollView;
@property (nonatomic,retain) UIScrollView *scrollViewOfAlbum;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self makeAlbum];
    [self addPage];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(CGRectGetWidth(self.view.bounds) / 2 - 7.5, 120);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.minimumLineSpacing = 5;
    layout.minimumInteritemSpacing = 5;
    layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    layout.headerReferenceSize = CGSizeMake(100, 100);
    layout.footerReferenceSize = CGSizeMake(200, 200);
    layout.sectionHeadersPinToVisibleBounds = YES;
    layout.sectionFootersPinToVisibleBounds = YES;
    UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
//    UICollectionView *myCollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
//    [self.view addSubview:myCollectionView];
//    myCollectionView.delegate = self;
//    myCollectionView.dataSource = self;
//    
//    [myCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"pool"];
//    [myCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"pool1"];
//    [myCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"pool2"];
}

- (void)makeAlbum {
    UIScrollView *album = [[UIScrollView alloc] initWithFrame:FRAME];
    [self.view addSubview:album];
    NSArray *arr = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", nil];
    album.contentSize = CGSizeMake(WIDTH * arr.count, HEIGHT);
    album.pagingEnabled = YES;
    album.bounces = NO;
    album.delegate = self;
    for (NSInteger i = 0; i < arr.count ; i++) {
        UIScrollView *smallScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, HEIGHT)];
        UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
        smallScrollView.delegate = self;
        img.image = [UIImage imageNamed:arr[i]];
        [smallScrollView addSubview:img];
        [album addSubview:smallScrollView];
        smallScrollView.tag = i;
        smallScrollView.minimumZoomScale = 0.5;
        smallScrollView.maximumZoomScale = 2;
    }
    album.contentOffset = CGPointMake(WIDTH, 0);
    album.bounces = NO;
    album.showsHorizontalScrollIndicator = NO;
}
- (void)addPage{
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(WIDTH / 2 - 50, HEIGHT - 80, 100, 40)];
    self.page.currentPage = 0;
    self.page.numberOfPages = 3;
    [self.view addSubview:self.page];
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    _index = scrollView.tag;
    return [scrollView.subviews firstObject];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
    self.page.currentPage = index - 1;
    if (index == 3) {
        self.page.currentPage = 0;
        scrollView.contentOffset = CGPointMake(0, 0);
    }
    if (index == 0) {
        self.page.currentPage = 3;
        scrollView.contentOffset = CGPointMake(WIDTH * 3, 0);
    }
    UIScrollView *scroll = [scrollView.subviews objectAtIndex:_index];
    scroll.zoomScale = 1;
}
- (void)pageAction:(UIPageControl *)page{
    // 点击换图
    [self.scrollViewOfAlbum setContentOffset:CGPointMake(WIDTH * (self.page.currentPage), 0) animated:YES];
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 3;
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 23;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor greenColor];
    return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]){
        UICollectionReusableView *reuesableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"pool1" forIndexPath:indexPath];
        reuesableView.backgroundColor = [UIColor yellowColor];
        return reuesableView;
    } else {
        UICollectionReusableView *reuesableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"pool2" forIndexPath:indexPath];
        reuesableView.backgroundColor = [UIColor purpleColor];
        return reuesableView;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
